<template>
    <div>
        <navtop title="我的订单" :fixed="true" />
        <div class="my-box">
            <div class="my-bg">
            </div>
        </div>
        <div class="bg-content">
            <van-tabs v-model:active="active" color="#0C34BA" @change="toggleTab" line-height="2" swipeable>
                <van-tab :title="item2.title" v-for="(item2, index) in tabsData" :key="index">
                    <div class="order-product" v-for="(item, index) in arr3 " :key="item">
                        <div class="info-name">订单信息</div>
                        <div class="info-order">
                            <div>订单编号：{{ item[1][0].oid }}</div>
                            <div v-if="item[1][0].status === 1" @click="confirm(item[1][0].oid, item, index)"
                                class="mybtn-order">
                                确认收货</div>
                            <div class="op" v-else>
                                <div class="finished">已完成</div>
                                <div class="remove" @click="removeOrder(item[1][0].oid, item, index)">
                                    <van-icon name="delete-o" />
                                </div>
                            </div>
                        </div>
                        <div class="shopbag-list-item" v-for="item1 in item[1]" :key="item1.sid">
                            <van-swipe-cell>
                                <div class="shp-item">
                                    <div class="shp-img">
                                        <img class="auto-img" :src="item1.smallImg" alt="">
                                    </div>
                                    <div class="shopbag-item-content">
                                        <div class="shopbag-name">
                                            <div class="zhname-box">
                                                <div class="zhname one-text">{{ item1.name }}</div>
                                                <div class="rule one-text">{{ item1.rule }}</div>
                                            </div>
                                            <div class="enname one-text">{{ item1.enname }}</div>
                                        </div>

                                        <div class="price-count">
                                            <div class="price">&yen;{{ item1.price }}</div>
                                            <div class="count">
                                                x{{ item1.count }}
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            </van-swipe-cell>
                        </div>
                        <div class="order-date">{{ item[1][0].createdAt }}</div>
                        <div class="order-total">
                            <div class="order-count">共计{{ item[0].num }} 件商品</div>
                            <div class="order-money">订单金额：&yen;{{ item[0].price }}</div>
                            <div class="left-circle circle"></div>
                            <div class="right-circle circle"></div>
                        </div>
                    </div>
                    <van-empty v-show="arr3.length === 0" description="没有订单数据！" />

                    <!-- <div class="order-product">111</div> -->
                </van-tab>

            </van-tabs>
        </div>


    </div>
</template>
<script setup lang="ts">

import navtop from "@/components/navtop.vue";
import { Toast, Dialog } from "vant";
// import { useRouter } from "vue-router";
import { getfindOrder, postreceive, postremoveOrder } from '@/api/index'
// import { result } from "lodash";
// const activeName = ref('a');


let productDetail: any = ref<Array<any>>([]);

let active: any = ref<number>(0);

// let arr: any = ref<Array<any>>([]);

let arr3: any = ref<Array<any>>([]);

let resArr = [];


let tabsData: any = ref<Array<any>>([
    {
        title: '全部'
    },
    {
        title: '进行中'
    },
    {
        title: '已完成'
    }
]);


function rTime(date) {
    var json_date = new Date(date).toJSON();
    return new Date(new Date(json_date).getTime() + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}

let confirm = (oid, item, index) => {
    // console.log(item);
    // console.log(index);
    Dialog.confirm({
        title: "收货提示",
        message: '是否确定收货！',
        confirmButtonColor: "#0C34BA",
    }).then((): void => {
        postreceive({ oid }).then((result: any): void => {


            Toast(result.data.msg);

            if (result.data.code === 700) {

                // router.push({ name: 'Login' });
                return;
            }

            if (result.data.code === 80000) {
                if (active.value === 0) {
                    item[1][0].status = 2;
                    // console.log(1111);

                } else if (item[1][0].status === 1) {
                    //删除页面的订单数据

                    // console.log(12);

                    arr3.value.splice(index, 1);
                }
            }


        })
    })



    // console.log(oid);

}

let removeOrder = (oid, item, index) => {
    console.log(item);

    Dialog.confirm({
        title: "删除订单",
        message: '是否确定删除该订单！',
        confirmButtonColor: "#0C34BA",
    }).then((): void => {
        postremoveOrder({ oid }).then((result: any): void => {


            Toast(result.data.msg);

            // if (result.data.code === 700) {

            //     router.push({ name: 'Login' });
            //     return;
            // }

            if (result.data.code === 90000) {
                arr3.value.splice(index, 1);
            }

        })
    })

}

let getOrderData = () => {
    arr3.value = [];
    resArr = [];
    getfindOrder({ status: active.value }).then((result) => {
        // let resArr1 = [];
        if (result.data.code == 70000) {
            // console.log(1);

            productDetail.value = result.data.result;
            resArr = result.data.result;
        }
        // console.log(resArr);

        // console.log(productDetail.value);

        let arr = []

        resArr.forEach((item) => {
            arr.push(item.oid)
        })

        // console.log(arr);
        arr = Array.from(new Set(arr))
        // let arr3 = [];
        // console.log(arr) // [ 1, 2, 'abc', true, false, undefined, NaN ]
        arr.forEach((itme1) => {
            let arr1 = [];
            let arr2 = [];
            let obj = {
                num: 0,
                price: 0
            };
            resArr.forEach((item) => {
                // arr.value.push(item.oid)
                if (itme1 == item.oid) {
                    item.createdAt = rTime(item.createdAt)
                    // console.log(index);
                    obj.num = item.count + obj.num;
                    obj.price = item.count * item.price + obj.price;
                    arr2.push(item)
                }
            })
            // console.log(arr2);
            arr1.push(obj)
            arr1.push(arr2)
            arr3.value.push(arr1);
        })
        // console.log(arr3.value.length);

    })
}

//切换订单状态
let toggleTab: () => void = () => {

    getOrderData();
}

onMounted(() => {

    getOrderData()
})

</script>

<style lang="less" scoped>
.my-box {
    width: 100vw;

    .my-bg {
        width: 100vw;
        height: 160px;
        background-position: center center;
        background-repeat: no-repeat;
        background-size: cover;
        background-color: #0c34ba;
        position: relative;
    }



    // .bg-cover {}
}

.bg-content {
    margin: 0 10px;
    position: relative;
    top: -20px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    overflow: hidden;

    .order-product {
        margin-top: 10px;
        margin-bottom: 10px;
        background-color: #fff;
        padding: 10px;

        .order-product {
            padding: 10px;
            margin-top: 30px;
            background-color: #fff;
            border-top-left-radius: 10px;
            border-top-right-radius: 10px;
        }

        .info-order {
            height: 47px;
            padding: 10px;
            color: #999;
            font-size: 14px;
            align-items: center;
            justify-content: space-between;
            display: flex;
            margin-bottom: 10px;

            .op {
                // width: 66px;
                display: flex;
                align-items: center;

                .remove {
                    margin-left: 5px;
                    width: 16px;
                    height: 16px;
                    font-size: 16px;
                }
            }

            .mybtn-order {
                border-radius: 10px;
                padding: 4px 8px 4px 8px;
                font-size: 14px;
                color: #ff0000;
                position: relative;
                left: 10px;
                text-align: center;
                // width: 66px;
                // background-color: #0c34ba;
                height: auto;
            }
        }

        .order-money {
            color: #e4393c;
            font-weight: bold;
            margin-left: auto;
        }

        .order-count {
            color: #666;
        }

        .order-date {
            padding: 10px 10px 0px 10px;
            font-size: 14px;
            color: #666;
        }

        .order-total {
            display: flex;
            font-size: 14px;
            align-items: center;
            padding: 10px;
            border-top: 1px dashed #e8e8e8;
            position: relative;


        }

        &:last-child {
            margin-bottom: 0;
        }

        .info-name {
            font-size: 14px;
            padding: 10px;
            font-weight: 700;
        }

        .shopbag-list-item {
            background-color: #fff;
            // margin-bottom: 10px;
            border-radius: 10px;
            overflow: hidden;

            &:last-child {
                margin-bottom: 0;
            }

            .mydelectbtn {
                height: 100%;
            }

            :deep(.van-button) {
                height: 100%;
            }

            .shp-item {
                width: 100%;
                display: flex;
                padding: 10px;
                align-items: center;

                .shp-img {
                    width: 80px;
                    height: 80px;
                    // margin-left: 10px;
                    margin-right: 10px;

                    .auto-img {
                        // border-radius: 5px;
                        width: 100%;
                    }
                }

                .shopbag-item-content {
                    width: calc(~"100%-40px");
                    height: 80px;
                    flex: 1;
                }

                .price {
                    font-size: 14px;
                    font-weight: bold;
                    color: #e4393c;
                    margin-right: auto;
                }

                .enname {
                    font-size: 12px;
                    color: #999;
                    margin-top: 3px;
                    text-overflow: ellipsis;
                    overflow: hidden;
                    white-space: nowrap;
                }

                .zhname {
                    font-size: 14px;
                    max-width: 100px;
                }

                .rule {
                    margin-left: 20px;
                    color: #666;
                    font-size: 14px;
                    max-width: 100px;
                    text-overflow: ellipsis;
                    overflow: hidden;
                    white-space: nowrap;
                }

                .shopbag-name {
                    height: 40px;
                }

                .price-count {
                    margin-right: 20px;
                    font-size: 12px;
                    height: 40px;
                    display: flex;
                    align-items: center;
                }

                .zhname-box {
                    display: flex;
                }
            }
        }
    }
}
</style>